<?php


namespace App\Repositories;


use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;

class MallOrdersHistoryRepository
{

    /**
     * 获取产品订单列表
     * @param $where
     * @param $page
     * @param $perPage
     * @return bool
     * @throws BasicException
     */
    public function MallOrderList($where, $page = 1, $perPage = 20)
    {
        $list = app('model_mall_orders_history')
            ->with('user_buyer:user_id,nickname')
            ->with('user_owner:user_id,nickname')
            ->with('mall_products:id,title,picture')
            ->with('mall_categories:id,title')
            ->orderBy('id', 'desc');
        if (!empty($where['where_order'])) {
            $list = $list->where($where['where_order']);
        }
        if (!empty($where['where_buyer'])) {
            $list = $list->whereHas('user_buyer', function ($query) use ($where) {
                $query->where($where['where_buyer']);
            });
        }

        if (!empty($where['where_owner'])) {
            $list = $list->whereHas('user_owner', function ($query) use ($where) {
                $query->where($where['where_owner']);
            });
        }

        if (!empty($where['where_product'])) {
            $list = $list->whereHas('mall_products', function ($query) use ($where) {
                $query->where($where['where_product']);
            });
        }
        $list = $list->paginate($perPage, ['*'], '', $page);
        return $list;
    }


    //定时任务统计不同类型商品购买金额
    public function crontabMallBuyStat($startTime, $endTime)
    {
        //获取靓号分类8下面的所有分类id
        $goodsCodeIds = $this->getAllNextId(8);

        $res = app('model_mall_orders_history')->select(DB::raw('sum(price) as sum_price'), 'category_id')->groupBy('category_id')->whereIn('type', [1, 2])->where('add_time', '>=', $startTime)->where('add_time', '<', $endTime)->get();
        $res =  empty($res) ? [] : $res->toArray();
        $dbData = [];
        $totalMoney = 0;
        foreach ($res as $k => $v) {
            if (in_array($v['category_id'], $goodsCodeIds)) {
                $v['category_id'] = 8;
            }

            $dbData[] = [
                'cate_id' => $v['category_id'],
                'money' => $v['sum_price'],
                'origin_data' => json_encode($res),
                'start_time' => $startTime,
                'end_time' => $endTime,
            ];
            $totalMoney += $v['sum_price'];
        }
        $dbData[] = [
            'cate_id' => -1,
            'money' => $totalMoney,
            'origin_data' => json_encode($res),
            'start_time' => $startTime,
            'end_time' => $endTime,
        ];
        app('model_mall_stat')->insert($dbData);
    }

    /**
     * 查询一级分类下的所有子集
     * @param array $id 一级类目id
     * @param array $data
     * @return array
     */
    public function getAllNextId($id, $data = [])
    {
        if (!is_array($id)) {
            $id = [$id];
        }
        $pids = app('model_mall_categories')->whereIn('parent_id', $id)->pluck('id')->toArray();
        if (count($pids) > 0) {
            foreach ($pids as $v) {
                $data[] = $v;
                $data = $this->getAllNextId($v, $data); //注意写$data 返回给上级
            }
        }
        if (count($data) > 0) {
            return $data;
        } else {
            return false;
        }
    }
}
